unit PermissaoEspecifica;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SqlExpr, Provider, DB, DBClient, DBLocal, DBLocalS, StdCtrls,
  ExtCtrls, DBCtrls, Grids, DBGrids, UClasseGeral, Buttons, FMTBcd;

type
  TfrmPermissaoEspecial = class(TForm)
    Panel1: TPanel;
    DtSrc: TDataSource;
    SQLC_Permissoes: TSQLClientDataSet;
    DSUser: TDataSource;
    DSGroup: TDataSource;
    SQLC_Usuarios: TSQLClientDataSet;
    SQLC_Grupos: TSQLClientDataSet;
    SQLC_UsuariosUSU_CODIGO: TStringField;
    SQLC_UsuariosUSU_NOME: TStringField;
    SQLC_GruposGRU_CODIGO: TIntegerField;
    SQLC_GruposGRU_DESCRICAO: TStringField;
    SQLC_Lista: TSQLClientDataSet;
    SQLC_PermissoesPE_CODIGO: TIntegerField;
    SQLC_PermissoesPE_USUARIOS: TStringField;
    SQLC_PermissoesPE_PERMITE: TStringField;
    SQLC_PermissoesPE_GRUPOS: TIntegerField;
    SQLC_PermissoesPE_FUNCAO: TIntegerField;
    SQLC_PermissoesPE_MENU: TIntegerField;
    DSLista: TDataSource;
    SQLC_ListaMEN_CAPTION: TStringField;
    SQLC_ListaFP_CODIGO: TIntegerField;
    SQLC_ListaFP_DESCRICAO: TStringField;
    SQLC_ListaFP_MENU: TIntegerField;
    SQLC_ListaMEN_CODIGO: TIntegerField;
    Panel3: TPanel;
    Panel4: TPanel;
    DBGrid2: TDBGrid;
    Label1: TLabel;
    Label2: TLabel;
    cbPermite: TCheckBox;
    btnPermitir: TButton;
    DBGrid1: TDBGrid;
    dblcUser: TDBLookupComboBox;
    dblcGroup: TDBLookupComboBox;
    SQLC_PermissoesCAPTION: TStringField;
    SQLC_PermissoesFUNCAO: TStringField;
    BitBtnCancelar: TBitBtn;
    procedure BitBtnCancelarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure SQLC_ListaAfterScroll(DataSet: TDataSet);
    procedure btnPermitirClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPermissaoEspecial: TfrmPermissaoEspecial;
  TxtSql: string;

implementation

uses DmGeral, menu;

{$R *.dfm}

procedure TfrmPermissaoEspecial.BitBtnCancelarClick(Sender: TObject);
begin
  (DtSrc.DataSet as TSQLClientDataSet).CancelUpdates;
  Close;
end;

procedure TfrmPermissaoEspecial.FormCreate(Sender: TObject);
begin
  if (frmMenu.Width<=Self.Width+200)
  or (frmMenu.Height<=Self.Height+150)then begin
    frmMenu.tvMenuVertical.Visible := false;
    frmMenu.ToolBar1.Visible := false;
    Self.Position := poMainFormCenter;
    Self.Top := 0;
  end
  else begin
    Self.Left:= 0;
    Self.Top := 0;
  end;

  TxtSql := (DtSrc.DataSet as TSQLClientDataSet).CommandText;
  if not(DSLista.DataSet.Active) then
    DSLista.DataSet.Open;
  if not(DtSrc.DataSet.Active) then DtSrc.DataSet.Open;
  if not(DSUser.DataSet.Active) then DSUser.DataSet.Open;
  if not(DSGroup.DataSet.Active) then DSGroup.DataSet.Open;
  DBGrid1.Refresh;
end;

procedure TfrmPermissaoEspecial.DBGrid1CellClick(Column: TColumn);
begin
  if DtSrc.DataSet.FieldByName('PE_PERMITE').AsString = 'S' then
    cbPermite.Checked := true
  else
    cbPermite.Checked := false;
end;

procedure TfrmPermissaoEspecial.SQLC_ListaAfterScroll(DataSet: TDataSet);
begin
  if DtSrc.DataSet.FieldByName('PE_PERMITE').AsString = 'S' then
    cbPermite.Checked := true
  else
    cbPermite.Checked := false;
end;

procedure TfrmPermissaoEspecial.btnPermitirClick(Sender: TObject);
var
  registros: integer;
begin
  with DtSrc.DataSet as TSQLClientDataSet do begin
    Close;
    CommandText := TxtSQL +
                   ' WHERE PE_FUNCAO='+QuotedStr(DSLista.DataSet.FieldByName('FP_CODIGO').AsString)+
                   ' AND PE_MENU='+QuotedStr(DSLista.DataSet.FieldByName('FP_MENU').AsString)+
                   ' AND (PE_USUARIOS='+QuotedStr(DSUser.DataSet.FieldByName('USU_CODIGO').AsString)+
                   ' OR PE_GRUPOS='+QuotedStr(DSGroup.DataSet.FieldByName('GRU_CODIGO').AsString)+')';
    Open;
    registros := RecordCount;
    if Registros < 1 then begin
      Insert;
      FieldByName('PE_CODIGO').AsInteger     := 0;
      FieldByName('PE_FUNCAO').AsInteger     := DSLista.DataSet.FieldByName('FP_CODIGO').AsInteger;
      FieldByName('PE_MENU').AsInteger       := DSLista.DataSet.FieldByName('MEN_CODIGO').AsInteger;
      if dblcUser.KeyValue <> null then
        FieldByName('PE_USUARIOS').AsVariant := dblcUser.KeyValue;
      if dblcGroup.KeyValue <> null then
        FieldByName('PE_GRUPOS').AsVariant   := dblcGroup.KeyValue;
    end
    else
      Edit;
    if cbPermite.Checked then
      FieldByName('PE_PERMITE').AsString   := 'S'
    else
      FieldByName('PE_PERMITE').AsString   := 'N';
    Post;
    ApplyUpdates(0);
    Close;
    CommandText := TxtSQL;
    Open;
  end;
  DBGrid1.Refresh;
end;

procedure TfrmPermissaoEspecial.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  frmMenu.tvMenuVertical.Visible := true;  //restaurar o menu Vertical
  frmMenu.ToolBar1.Visible := true;

  Action := caFree;
  frmPermissaoEspecial := nil;
end;

end.
